package com.example.csmall.product.controller;

import com.example.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import com.example.csmall.product.pojo.dto.BrandAddNewDTO;
import com.example.csmall.product.pojo.dto.BrandUpdateDTO;
import com.example.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import com.example.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import com.example.csmall.product.pojo.vo.BrandListItemVO;
import com.example.csmall.product.pojo.vo.BrandStandardVO;
import com.example.csmall.product.service.BrandService;
import com.example.csmall.product.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Validated
@Api(tags = "04 品牌")
@RequestMapping("/brands")
public class BrandController {

    @Autowired
    BrandService service;

    @ApiOperation("添加品牌")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddNewDTO){
        service.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除品牌")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        service.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改品牌")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateInfoById(@PathVariable Long id, BrandUpdateDTO brandUpdateDTO){
        service.updateInfoById(id,brandUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id启用品牌")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        service.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id禁用品牌")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id){
        service.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("品牌列表")
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list(){
        List<BrandListItemVO> list = service.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id查询品牌")
    @GetMapping("/{id}")
    public JsonResult<BrandStandardVO> getStandardById(@PathVariable Long id){
        BrandStandardVO list = service.getStandardById(id);
        return JsonResult.ok(list);
    }

    @GetMapping("/rebuild-cache")
    @ApiOperation("更新brand缓存")
    public JsonResult<Void> BrandrebuildCache(){
        //手动执行调用service方法中的方法实现缓存预热功能
        service.rebuildCache();
        return JsonResult.ok();
    }

}
